var Sphere = function(center, radius) {
	this.center = center;
	this.radius = radius;
};

Sphere.prototype.initialize = function() {
	this.sqrRadius = this.radius * this.radius;
};

Sphere.prototype.intersect = function(ray) {
	var v = ray.origin.subtract(this.center);
	var DdotV = ray.direction.dot(v);
	if (DdotV > 0) {
		return IntersectResult.noHit;
	}
	var a0 = v.sequare() - this.sqrRadius;
	var discr = DdotV * DdotV - a0;
	if (discr < 0) {
		return IntersectResult.noHit;
	}
	var result = new IntersectResult();
	result.geometry = this;
	result.distance = -DdotV - Math.sqrt(discr);
	result.position = ray.getPoint(result.distance);
	result.normal = result.position.subtract(this.center).normalize();
	return result;
};

var IntersectResult = function(){
	this.geometry = null;
	this.distance = 0;
	this.position = Vector3.zero;
	this.normal = Vector3.zero;
};


var Plane = function(normal, d) {
	this.normal = normal;
	this.d = d;
};

Plane.prototype.initialize = function() {
	this.position = this.normal.multiply(this.d);
};

Plane.prototype.intersect = function(ray) {
	var a = ray.direction.dot(this.normal);
	if(a >= 0) {
		return IntersectResult.noHit;
	}
	var d = this.normal.dot(ray.origin.subtract(this.position));
	var result = new IntersectResult();
	result.geometry = this;
	result.distance = -d / a;
	result.position = ray.getPoint(result.distance);
	result.normal = this.normal;
	return result;
};

IntersectResult.noHit = new IntersectResult();